package com.budwk.app.sqfwV2.component;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.common.component.DictComponent;
import com.budwk.app.common.config.AppException;
import com.budwk.app.sqfw.constants.DictConstants;
import com.budwk.app.sqfw.dto.PersonnelTagDTO;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.models.PersonInfo;
import com.budwk.app.sqfw.services.PersonInfoService;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwV2.dto.dto.PersonInfoDTO;
import com.budwk.app.sqfwV2.enums.FileServiceConfigEnum;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 儿童探访记录公共业务类
 */

@Slf4j
@IocBean
public class ChildrenVisitationComponent {

    public static final String CHILDREN_TYPE = "儿童探访类别";

    public static BaseTemplateExcelGenerator getService(String code) {
        FileServiceConfigEnum config = FileServiceConfigEnum.findByCode(SupportBusinessTypeEnum.CHILDREN_VISITATION.getName(), code);
        if (config == null) {
            throw new AppException("请先配置文件信息！");
        }
        return StrUtil.getNewClass(config.getService());
    }

    /**
     * 提交人员基本信息
     * 主要组装类型和标签
     * @param reqVo
     */
    public static PersonInfoDTO assemblePerson(PersonInfoDTO reqVo) {
        reqVo.setIdType("身份证");
        PersonInfoService personInfoService = NutzIocUtils.getBean(PersonInfoService.class);
        if (StringUtils.isEmpty(reqVo.getId())) {
            reqVo.setBusinessType(SupportBusinessTypeEnum.CHILDREN_VISITATION.getName())
                    .setSpecialMark(buildSpecialMark(reqVo, null));
            return reqVo;
        }
        PersonInfo personInfo = personInfoService.fetch(reqVo.getId());
        log.info("child saveOrUpdatePerson queryPerson: {}", JSONObject.toJSONString(personInfo));
        if (personInfo == null) {
            throw new AppException("用户不存在！");
        }
        reqVo.setBusinessType(PersonInfoComponent.getBusinessType(personInfo.getBusinessType(), reqVo.getBusinessType()))
                .setSpecialMark(buildSpecialMark(reqVo, personInfo.getSpecialMark()));
        personInfoService.updateIgnoreNull(reqVo);
        return reqVo;
    }

    public static List<PersonnelTagDTO> buildSpecialMark(PersonInfoDTO reqVo, List<PersonnelTagDTO> oldSpecialMark) {
        List<PersonnelTagDTO> specialMarks = buildInitSpecialMark(reqVo);
        if (CollectionUtil.isEmpty(oldSpecialMark)) {
            return specialMarks;
        }
        oldSpecialMark.removeIf(e -> e.getBusinessType().equals(SupportBusinessTypeEnum.CHILDREN_VISITATION.getName()));
        oldSpecialMark.addAll(specialMarks);
        return oldSpecialMark;
    }

    public static List<PersonnelTagDTO> buildInitSpecialMark(PersonInfoDTO reqVo) {
        List<PersonnelTagDTO> list = new ArrayList<>();
        PersonnelTagDTO p = new PersonnelTagDTO();
        p.setBusinessType(SupportBusinessTypeEnum.CHILDREN_VISITATION.getName());
        p.setCode(SupportBusinessTypeEnum.CHILDREN_VISITATION.getCode());
        if (StringUtils.isEmpty(reqVo.getChildrenType())) {
            list.add(p);
            return list;
        }
        String dictName = DictComponent.getDictName(reqVo.getChildrenType(), null);
        List<PersonnelTagDTO.Dict> tag = new ArrayList<>();
        PersonnelTagDTO.Dict d = new PersonnelTagDTO.Dict();
        d.setBusinessType(CHILDREN_TYPE);
        d.setName(reqVo.getChildrenType().equals(DictConstants.CHILDREN_OTHER) ? reqVo.getOtherCategory() : dictName);
        d.setKey(reqVo.getChildrenType());
        tag.add(d);
        p.setTag(tag);

        list.add(p);
        return list;
    }
}
